from flask import Flask,session, g
import config
from exts import db,mail
from models import UserModel
from blueprints.qa import bp as qa_bp
from blueprints.auth import bp as auth_bp
from flask_migrate import Migrate

app = Flask(__name__)
#绑定配置文件
app.config.from_object(config)

db.init_app(app)#在这里进行绑定
mail.init_app(app)

migrate = Migrate(app, db)
app.register_blueprint(auth_bp)
app.register_blueprint(qa_bp)

#blueprint：用来做模块化的
#好的编程习惯：模块（电影，音乐，，，，）
# @app.route('/')
# def hello_world():  # put application's code here
#     return 'Hello World!'

#hook
@app.before_request
def my_before_request():
    user_id = session.get("user_id")
    if user_id:
        user = UserModel.query.get(user_id)
        setattr(g, 'user', user)
    else:
        setattr(g, 'user', None)#为了不要报错

@app.context_processor
def my_context_processor():
    return {"user": g.user}



if __name__ == '__main__':
    app.run()
